﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DrugStore.Models;
using DrugStore.Helper;

namespace DrugStore.Controllers
{
    public class ManageDiseaseController : Controller
    {
        //
        // GET: /ManageDisease/
        [MyAuthorize(LoginPage = "/Admin/LogOn")]
        public ActionResult Index(int pageIndex=1,string message = "",string keyword="",string field="",string orderField="",string direction="")
        {
            try
            {
                ViewData["message"] = message;
                ManageDiseaseBusiness bussiness = new ManageDiseaseBusiness();
                PaginateList<Disease> diseases = bussiness.Search(keyword, field, pageIndex, 15, orderField, direction);
                return View(diseases);
            
            }
            catch (Exception ex) {
                ViewData["message"] = ex.Message;
            }
            return View();                      
        }
        [MyAuthorize(LoginPage = "/Admin/LogOn")]
        public ActionResult Add()
        {
            try 
            {
                ManageDiseaseBusiness bussiness = new ManageDiseaseBusiness();
                ManageDiseaseModels model = new ManageDiseaseModels();

                //get selected categories and all category
                ManageNewBusiness newsBusiness = new ManageNewBusiness();
                List<Category> categories = newsBusiness.GetAllCategory();
                if (categories == null)
                    model.NewsCategories = new List<Category>();
                else model.NewsCategories = categories;
                model.SelectedNewsCategories = new List<String>();
                //model.DiseaseCategoryId = "3c88de14-aadf-4695-b3d4-5f50f4b41e21";

                model.setCategories(bussiness.GetCategories());
                DrugStoreEntities entities = new DrugStoreEntities();
                return View(model);
            }
            catch (System.Exception ex) 
            {
                ViewData["message"] = ex.Message;
            }
            return View();
        }

        [HttpPost]
        [ValidateInput(false)]
        [MyAuthorize(LoginPage = "/Admin/LogOn")]
        public ActionResult Add(ManageDiseaseModels model, HttpPostedFileBase diseasePath) 
        {
            try 
            {
                ManageDiseaseBusiness bussiness = new ManageDiseaseBusiness();

                // get all category
                ManageNewBusiness newsBusiness = new ManageNewBusiness();
                List<Category> categories = newsBusiness.GetAllCategory();
                if (categories == null)
                    model.NewsCategories = new List<Category>();
                else model.NewsCategories = categories;

                if (model.SelectedNewsCategories == null)
                {
                    model.SelectedNewsCategories = new List<string>();
                }

                try 
                {
                    model.setCategories(bussiness.GetCategories());
                }
                catch (Exception ex) 
                {
                    ModelState.AddModelError("",ex.Message);
                }

                if (ModelState.IsValid) 
                {
                    if (diseasePath == null)
                    {
                        throw new Exception("Bạn chưa tạo ảnh đại diện");
                    }
                    String image = Guid.NewGuid().ToString();
                    String ext = diseasePath.FileName.Substring(diseasePath.FileName.LastIndexOf("."));
                    String path = HttpContext.Server.MapPath("/Content/diseaseImages/"+image+ext);
                    diseasePath.SaveAs(path);
                    model.ImgPatch = "/Content/diseaseImages/" + image + ext;                                            
                    bussiness.AddDisease(model);
                    ViewData["message"] = "Thêm bệnh án thành công";
                }
            }
            catch (System.Exception ex) 
            {
                ModelState.AddModelError("", ex.Message);
            }
           return View(model);
        }

        [ValidateInput(false)]
        [MyAuthorize(LoginPage = "/Admin/LogOn")]
        public ActionResult Edit(String id) 
        {
            try 
            {
                ManageDiseaseBusiness business = new ManageDiseaseBusiness();
                ManageDiseaseModels model = business.GetDiseaseById(id);

                ManageNewBusiness newsBusiness = new ManageNewBusiness();
                List<Category> categories = newsBusiness.GetAllCategory();
                if (categories == null)
                    model.NewsCategories = new List<Category>();
                else model.NewsCategories = categories;

                model.setCategories(business.GetCategories());
                return View(model);
            }
            catch (Exception ex) 
            {
                ModelState.AddModelError("",ex.Message);
            }
            return View();
        }

        [HttpPost]
        [ValidateInput(false)]
        [MyAuthorize(LoginPage = "/Admin/LogOn")]
        public ActionResult Edit(String id,ManageDiseaseModels model, HttpPostedFileBase diseasePath) {
            ManageDiseaseBusiness business = new ManageDiseaseBusiness();
            try 
            {
                if (ModelState.IsValid) 
                {
                    
                    if (diseasePath != null) 
                    {
                        String diseaseId = id;
                        String ext = diseasePath.FileName.Substring(diseasePath.FileName.LastIndexOf("."));
                        String path = HttpContext.Server.MapPath("/Content/diseaseImages/" + diseaseId + ext);
                        diseasePath.SaveAs(path);
                        model.ImgPatch = "/Content/diseaseImages/" + diseaseId + ext;
                    }
                    model.DiseaseId = id;
                    business.UpdateDisease(model);
                    return RedirectToAction("Index");                    
                }
            }
            catch (Exception ex) 
            {
                ModelState.AddModelError("", ex.Message);
            }
            // get all category
            ManageNewBusiness newsBusiness = new ManageNewBusiness();
            List<Category> categories = newsBusiness.GetAllCategory();
            if (categories == null)
                model.NewsCategories = new List<Category>();
            else model.NewsCategories = categories;

            if (model.SelectedNewsCategories == null)
            {
                model.SelectedNewsCategories = new List<string>();
            }
            model.setCategories(business.GetCategories());
            return View(model);
        }

        public ActionResult Delete(String id) 
        {
            try
            {

            }
            catch (Exception ex) 
            {
            
            }
            return View();
        }
    }
}
